#include "global.h"
#include "bonusMain.h"
#include "toolkit.h"
#include <wx/msgdlg.h>
#include <wx/panel.h>
#include "DoubleBallGridCellRenderer.h"
#include <wx/event.h>
#include <wx/splitter.h>

void initGridByDB(wxGrid * Grid1, char* SQL)
{
     int gridLastRow=0;
  		try
    	{
 DoubleBall* db= new DoubleBall();
      db->query(SQL);
if(db->records==NULL)
     {
          wxMessageBox(wxT("没有任何记录"),wxT("Warning!"));
//           exit(0);
       delete db;
        return;
        }
        for(int i=1;i<=db->nrow;i++)//line,注意，recoreds包含了列名字
           {
            Grid1->AppendRows(1,true);
            gridLastRow = (Grid1->GetNumberRows());
            for(int j=1;j<db->ncol;j++) //第一列是rowid,去掉
           {
                   if(j>2&& j<(db->ncol-1))//add red ball
                    {
                Grid1->SetCellValue(gridLastRow-1,atoi(db->records[i*db->ncol+j])+1,wxString::Format(wxT("%02d"),atoi(db->records[i*db->ncol+j])));
 //               Grid1->SetCellEditor(gridLastRow-1,j,new wxGridCellNumberEditor(1,33));
                     }else if(j==(db->ncol-1))//add blue;
                      {
                Grid1->SetCellValue(gridLastRow-1,35,wxString::Format(wxT("%02d"),atoi(db->records[i*db->ncol+j])));
//                Grid1->SetCellEditor(gridLastRow-1,35,new wxGridCellNumberEditor(1,16));
                       }else
            {
                //skip datatime
              //  if(j!=2)
                Grid1->SetCellValue(gridLastRow-1,j-1,wxString(db->records[i*db->ncol+j],wxConvUTF8));
            }
           }
         }
  //last term init to LAST_TERM
  DBGrid::LAST_TERM.reset();
      DBGrid::LAST_TERM.term =wxString::Format(wxT("%02d"),atoi(db->records[db->nrow*db->ncol+1]));
      DBGrid::LAST_TERM.date =wxString::Format(wxT("%02d"),atoi(db->records[db->nrow*db->ncol+2]));
      DBGrid::LAST_TERM.redBall1  =atoi(db->records[db->nrow*db->ncol+3]);
      DBGrid::LAST_TERM.redBall2  =atoi(db->records[db->nrow*db->ncol+4]);
      DBGrid::LAST_TERM.redBall3  =atoi(db->records[db->nrow*db->ncol+5]);
      DBGrid::LAST_TERM.redBall4  =atoi(db->records[db->nrow*db->ncol+6]);
      DBGrid::LAST_TERM.redBall5  =atoi(db->records[db->nrow*db->ncol+7]);
      DBGrid::LAST_TERM.redBall6  =atoi(db->records[db->nrow*db->ncol+8]);
      DBGrid::LAST_TERM.blueBall  =atoi(db->records[db->nrow*db->ncol+9]);
      DBGrid::LAST_TERM.ployName=wxEmptyString;//2?????       doubleball();
//      wxMessageBox(arrary2string(GlobalEvtHandler::LAST_TERM.redBall()));
 //over
    delete db;
    	}catch(...)
    	 {
    	     wxMessageBox(wxT("警告"),wxT("数据库问题"));
    	 }
}
/*
建立内容grid,
*/
void createHistoryGrid(bonusFrame* frame)
{

frame->clearMainPanel();
 //wxMessageBox(wxString::Format(wxT("Address:%x"),frame));
   frame->Panel1=new wxPanel(frame, frame->ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxWANTS_CHARS, wxT("ID_frame->Panel1"));

   frame->split=new wxSplitterWindow(frame->Panel1, wxID_ANY,
                                   wxDefaultPosition, wxDefaultSize,
                                   wxSP_3D | wxSP_LIVE_UPDATE |
                                   wxCLIP_CHILDREN /* | wxSP_NO_XP_THEME */ );
    frame->split->SetSashGravity(1.0);
    frame->split->SetMinimumPaneSize(0);
   createSystemLogger(frame);

    int bestsize=11;

//    wxSize defaultsize(11,11);
    wxFont* gFont= new wxFont(10, wxDEFAULT, wxNORMAL, wxNORMAL, 0, wxT("Sans"));
    //new wxFont(defaultsize,wxFONTFAMILY_DEFAULT ,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,wxT("SIMSUN"),wxFONTENCODING_DEFAULT );
    //gFont->SetWeight(wxFONTWEIGHT_BOLD);

    frame->Grid1 = new DBGrid(frame->split, frame->ID_GRID1, wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS, wxT("ID_frame->Grid1"));//wxPoint(56,24)
//    frame->Grid1->Connect(wxEVT_GRID_EDITOR_CREATED,wxCommandEventHandler(GlobalEvtHandler::GridArrowEvtHandler));
    frame->Grid1->SetDefaultCellFont(*gFont);
    //dirttable need flag
    frame->Grid1->SetDefaultRenderer(new DoubleBallGridCellRenderer());
    frame->Grid1->CreateGrid(0,36,wxGrid::wxGridSelectRows);
    frame->Grid1->SetMinSize(wxSize(0,0));
    frame->Grid1->EnableEditing(true);
    frame->Grid1->EnableGridLines(true);
    frame->Grid1->SetRowLabelSize(0);

    frame->Grid1->SetColLabelValue(0, wxT("期数"));
    frame->Grid1->SetColLabelValue(1, wxT("时间"));

    frame->Grid1->SetColMinimalWidth(1,0);
    frame->Grid1->SetColMinimalAcceptableWidth(0);//关键，否则wxgrid setColSize为0，将不工作

    frame->Grid1->SetColLabelValue(2, wxT("01"));
    frame->Grid1->SetColLabelValue(3, wxT("02"));
    frame->Grid1->SetColLabelValue(4, wxT("03"));
    frame->Grid1->SetColLabelValue(5, wxT("04"));
    frame->Grid1->SetColLabelValue(6, wxT("05"));
    frame->Grid1->SetColLabelValue(7, wxT("06"));
    frame->Grid1->SetColLabelValue(8, wxT("07"));
    frame->Grid1->SetColLabelValue(9, wxT("08"));
    frame->Grid1->SetColLabelValue(10, wxT("09"));
    frame->Grid1->SetColLabelValue(11, wxT("10"));
    frame->Grid1->SetColLabelValue(12, wxT("11"));
    frame->Grid1->SetColLabelValue(13, wxT("12"));
    frame->Grid1->SetColLabelValue(14, wxT("13"));
    frame->Grid1->SetColLabelValue(15, wxT("14"));
    frame->Grid1->SetColLabelValue(16, wxT("15"));
    frame->Grid1->SetColLabelValue(17, wxT("16"));
    frame->Grid1->SetColLabelValue(18, wxT("17"));
    frame->Grid1->SetColLabelValue(19, wxT("18"));
    frame->Grid1->SetColLabelValue(20, wxT("19"));
    frame->Grid1->SetColLabelValue(21, wxT("20"));
    frame->Grid1->SetColLabelValue(22, wxT("21"));
    frame->Grid1->SetColLabelValue(23, wxT("22"));
    frame->Grid1->SetColLabelValue(24, wxT("23"));
    frame->Grid1->SetColLabelValue(25, wxT("24"));
    frame->Grid1->SetColLabelValue(26, wxT("25"));
    frame->Grid1->SetColLabelValue(27, wxT("26"));
    frame->Grid1->SetColLabelValue(28, wxT("27"));
    frame->Grid1->SetColLabelValue(29, wxT("28"));
    frame->Grid1->SetColLabelValue(30, wxT("29"));
    frame->Grid1->SetColLabelValue(31, wxT("30"));
    frame->Grid1->SetColLabelValue(32, wxT("31"));
    frame->Grid1->SetColLabelValue(33, wxT("32"));
    frame->Grid1->SetColLabelValue(34, wxT("33"));
    frame->Grid1->SetColLabelValue(35, wxT("篮球"));

    frame->Grid1->SetSelectionMode(wxGrid::wxGridSelectRows);//整行选择

     frame->Grid1->ForceRefresh();
     frame->Grid1->EnableDragColSize(true);

    //set red
     wxGridCellAttr *attr = new wxGridCellAttr();
     attr->SetFont(*gFont);
     attr->SetTextColour(wxColour(255,0,0));
 //change date time
        frame->Grid1->SetColAttr(1, attr);
       frame->Grid1->SetColSize(1,0);

     for(int i=2;i<35;i++)
     {
       frame->Grid1->SetColAttr(i, attr);
       frame->Grid1->SetColSize(i,20);

     }

    //set blue
     wxGridCellAttr *attrRed = new wxGridCellAttr();
     attrRed->SetTextColour(wxColour(0,0,255));
     frame->Grid1->SetColAttr(35, attrRed);



//初始化grid用数据

  initGridByDB(frame->Grid1,DoubleBall::QUERY_DB);
  //add for new term forcast
  frame->Grid1->AppendRows(1,true);
  //keep data table
  if(DoubleBallGridCellRenderer::DIRTTABLE==NULL)DoubleBallGridCellRenderer::DIRTTABLE=new wxGridStringTable(frame->Grid1->GetNumberRows(),frame->Grid1->GetNumberCols());
  for(int i=0;i<frame->Grid1->GetNumberRows();i++)
      for(int j=0;j<frame->Grid1->GetNumberCols();j++)
      {
        DoubleBallGridCellRenderer::DIRTTABLE->SetValue(i,j,frame->Grid1->GetTable()->GetValue(i,j));
      }


  //更新第一个字段和第二个字段
  wxString nextTermDate=getNextTermDate(wxEmptyString);
  wxString nextTermOrder=getNextTermOrder(wxEmptyString);
   int lastRow=frame->Grid1->GetNumberRows();
   frame->Grid1->SetCellValue(lastRow-1,0,nextTermOrder);
   frame->Grid1->SetCellValue(lastRow-1,1,nextTermDate);

  frame->Grid1->ForceRefresh();
  int maxrow=frame->Grid1->GetNumberRows();
  int maxcol=0;
  frame->Grid1->MakeCellVisible(maxrow-1,0);

  //frame->Grid1->MakeCellVisible(maxrow-1,maxcol);
      //frame->Grid1->Fit();
//location,设置最小窗口大小

     frame->split->Initialize(frame->Grid1);
    frame->split->SplitHorizontally(frame->Grid1, frame->logWin);

    wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );

    //topSizer->Add( frame->Grid1,1,wxALIGN_CENTER_HORIZONTAL | wxALL | wxEXPAND,5 );
    topSizer->Add( frame->split,1,wxALIGN_CENTER_HORIZONTAL | wxALL | wxEXPAND,5 );
 //   topSizer->Add( frame->logWin,0,wxEXPAND );

//    topSizer->SetMinSize(getScreenWidth(),getScreenHeight());

    frame->Panel1->SetSizer( topSizer );
    frame->SendSizeEvent();
    topSizer->SetSizeHints(frame->Panel1);
    frame->SendSizeEvent();

//        DoubleBallGridCellRenderer::FILLEDBLANK=false;
}
//建立系统logger
void createSystemLogger(bonusFrame* frame)
{

//add log wxDisplay::
    int gridW =getScreenWidth(), gridH =getScreenHeight()-50 ;
    int logW = gridW, logH = 30;

    frame->logWin = new wxTextCtrl( frame->split,
                             wxID_ANY,
                             wxEmptyString,
                             wxPoint( 0, gridH + 80 ),
                             wxSize( logW, logH ),
                             wxTE_MULTILINE);
    frame->logger = new wxLogTextCtrl( frame->logWin );
    frame->m_logOld = wxLog::SetActiveTarget( frame->logger );

    wxLog::SetTimestamp(wxT("%Y-%m-%d %H:%M:%S"));
}

